// quantity of Alien Dogs
int cntDA = 0;
int cntADA = 0;
int cntTDA = 0;
int cntWDA = 0;
int cntTotal = 0;

final int dragRange = 60; // mouse cursor close detect range
final int bloodMax = 300;

// class dog alien
class cdogAlien {
  cdogAlien(int x, int y, int id, PFont font) {
    this.x = x;
    this.y = y;
    this.id = id;
    this.font = font;

    h = (int)random(0, 360);
    s = (int)random(80, 100);
    b = (int)random(70, 90);

    // assign blood value
    blood = 250; // a little bit lower value

    // assign a planet
    planet = planets[(int)random(planets.length)];

    cntDA++;
    cntTotal++;
  }

  void paint() {
    fill(h, s, b);
    stroke(h, s, b);

    rect(x, y, 90, 55); // body
    circle(x - 45, y - 28, 40); // head
    line(x - 26, y + 28, x -45, y + 51); // front leg
    line(x + 29, y + 28, x + 45, y + 48); // back leg

    textFont(font);
    textSize(20);
    text(nf(id, 2, 0), x - 15, y - 30); // show ID

    textFont(font1);
    textSize(20);
    text(nf(blood, 0, 0) + " / " + nf(bloodMax, 0, 0), x - 15, y + 58);

    /*
    // show HSB for debug
     text(h, x + 50, y);
     text(s, x + 50, y + 20);
     text(b, x + 50, y + 40);
     */
  }

  boolean mouseInRange(int mx, int my) {
    return (abs(mx - x) < dragRange && abs(my - y) < dragRange);
  }

  void bark() {
    sndBark1.play();
  }

  // show planet, show blood, show blood bar
  void showDetail() {
    int showx, showy;

    if (x > (width - 200)) {
      showx = x - 130;
    } else {
      showx = x + 120;
    }

    if (y > (height - 50)) {
      showy = y - 150;
    } else {
      showy = y + 5;
    }

    fill(h, s, b, 150);
    stroke(h, s, b, 160);
    rect(showx, showy, 100, 80);
    fill(#FFFFFF);
    textFont(font1);
    textSize(17);
    String str = planet + "\n" + nf(blood, 0, 0);
    text(str, showx - 40, showy - 12);

    // blood bar
    rectMode(CORNER);
    fill(h, s, b, 190);
    rect(showx - 45, showy + 33, map(blood, 0, 300, 0, 90), 7);
    rectMode(CENTER);
  }

  int x, y;
  int id;
  PFont font;

  // color HSB
  int h, s, b;

  String planet; // planet dog comes from
  int blood; // blood value [0, 300]
};


// class Antenna Dog Alien
class cantennaDogAlien extends cdogAlien {
  cantennaDogAlien(int x, int y, int id, PFont font) {
    super(x, y, id, font);
    cntDA--;
    cntADA++;
  }

  void paint() {
    super.paint();
    line(x - 60, y - 43, x - 88, y - 64); // antenna
  }

  void bark() {
    sndBark2.play();
  }
};

// class Tail Dog Alien
class ctailDogAlien extends cantennaDogAlien {
  ctailDogAlien(int x, int y, int id, PFont font) {
    super(x, y, id, font);
    cntADA--;
    cntTDA++;
  }

  void paint() {
    super.paint();

    textFont(font4);
    textSize(42);
    text("S", x + 40, y - 20); // tail
  }

  void bark() {
    sndBark3.play();
  }
};

// class Wing Dog Alien
class cwingDogAlien extends cdogAlien {
  cwingDogAlien(int x, int y, int id, PFont font) {
    super(x, y, id, font);
    cntDA--;
    cntWDA++;
  }

  void paint() {
    // wing
    fill(#FFFFFF);
    stroke(#FFFFFF);
    triangle(x - 7, y - 24, x + 5, y - 62, x + 47, y - 80);

    super.paint();
  }

  void bark() {
    sndBark4.play();
  }
};

// ========================== //
// all Dog Alien in 1 array
cdogAlien[] allDogAlien= new cdogAlien[0];

// add 1 any kind of Dog Alien to array
void addDogAlien(cdogAlien i) {
  allDogAlien = (cdogAlien[])(append(allDogAlien, i)); // must use 'cdogAlien[]' explicit cast
}

// new a kind of Dog Alien (4 types)
void newDogAlien() {
  addDogAlien(new cdogAlien(width/2, height - 80, cntTotal + 1, font1));
}

void newAntennaDogAlien() {
  addDogAlien(new cantennaDogAlien(width/2, height - 80, cntTotal + 1, font2));
}

void newTailDogAlien() {
  addDogAlien(new ctailDogAlien(width/2, height - 80, cntTotal + 1, font3));
}

void newWingDogAlien() {
  addDogAlien(new cwingDogAlien(width/2, height - 80, cntTotal + 1, font4));
}
